function logFillPage() {
	$("#pageContent").hide();
	$("#logLoading").show();
	var idUser = $("#loggedUserId").val();
	var sLogin = $("#loggedUserLogin").val();
	if (idUser > 0) {
		// ON MET LE LOGGIN DU USER DANS LE DROPDOWN DU HEADER
		if (sLogin != null && sLogin != '') {
			$("#dropdownUserLogin").html(
					sLogin + " <b class=\"caret\"></b>");
		}
		// ON RECUPERE LES GROUPES DE L'UTILISATEUR
		getUserGroup(idUser);
		// ON RECUPERE LES MODULES DE L'UTILISATEUR
		getUserModule(idUser);
		// ON RECUPERE LES RENDUS DE L'UTILISATEUR
		getUserRender(idUser);
		// ON REMPLIT LES SELECT BOX DE CREATION DE USER
		getSelectBoxes();

		$("#logLoading").hide();
		$("#pageContent").fadeIn();
	} else {
		// ICI IL Y A UNE ERREUR IDUSER < 0
	}

}

function getSelectBoxes() {
	$
			.ajax({
				type : "GET",
				url : "getSelectBoxes",
				dataType : "json",
				success : function(returnValue) {
					if (returnValue != null) {
						if (returnValue.entities != null
								&& returnValue.entities.length > 0) {
							$("#userCreateEntity").empty();
							for (var i = 0; i < returnValue.entities.length; i++) {
								$("#userCreateEntity")
										.append(
												'<option id="'
														+ returnValue.entities[i].idEntity
														+ '">'
														+ returnValue.entities[i].sEntityName
														+ '</option>');
							}
						}
						if (returnValue.rights != null
								&& returnValue.rights.length > 0) {
							$("#userCreateRight").empty();
							for (var i = 0; i < returnValue.rights.length; i++) {
								$("#userCreateRight")
										.append(
												'<option id="'
														+ returnValue.rights[i].idRight
														+ '">'
														+ returnValue.rights[i].sRightName
														+ '</option>');
							}
						}
						if (returnValue.status != null
								&& returnValue.status.length > 0) {
							$("#userCreateStatus").empty();
							for (var i = 0; i < returnValue.status.length; i++) {
								$("#userCreateStatus")
										.append(
												'<option id="'
														+ returnValue.status[i].idStatus
														+ '">'
														+ returnValue.status[i].sNameStatus
														+ '</option>');
							}
						}
					}
					return returnValue; // with return value excecute code
				},
				error : function(request, error) {
					;
				}
			});
}

function getUserGroup(idUser) {
	if (idUser > 0) {
		$.ajax({
			type : "GET",
			url : "usergroup",
			dataType : "json",
			data : {
				idUser : idUser
			},
			success : function(returnValue) {
				$("#groupResultRow").empty();
				var taille = returnValue.result.length;
				$.jStorage.set('userGroups', returnValue.result);
				var i = 0;
				for (i = 0; i < taille; i++) {
					$("#groupResultRow").append(
							printGroupResult(returnValue.result[i], i, 'userGroups'));
				}
				return returnValue; // with return value excecute code
			},
			error : function(request, error) {
				alert('error');
			}
		});
	}
}

function getUserModule(idUser) {
	if (idUser > 0) {
		$.ajax({
			type : "GET",
			url : "usermodule",
			dataType : "json",
			data : {
				idUser : idUser
			},
			success : function(returnValue) {
				$("#moduleResultRow").empty();
				var taille = returnValue.result.length;
				$.jStorage.set('userModules', returnValue.result);
				var i = 0;
				for (i = 0; i < taille; i++) {
					$("#moduleResultRow").append(
							printModuleResult(returnValue.result[i], i, 'userModules'));
				}
				return returnValue; // with return value excecute code
			},
			error : function(request, error) {
				alert('error');
			}
		});
	}
}

function getUserRender(idUser) {
	if (idUser > 0) {
		$.ajax({
			type : "GET",
			url : "userrender",
			dataType : "json",
			data : {
				idUser : idUser
			},
			success : function(returnValue) {
				$("#renderResultRow").empty();
				var taille = returnValue.result.length;
				$.jStorage.set('userRenders', returnValue.result);
				var i = 0;
				for (i = 0; i < taille; i++) {
					$("#renderResultRow").append(
							printRenderResult(returnValue.result[i], i, 'userRenders'));
				}
				return returnValue; // with return value excecute code
			},
			error : function(request, error) {
				alert('error');
			}
		});
	}
}

function removeObject(idObject, objectType) {
	if (idObject > 0 && objectType != null) {
		$.ajax({
			type : "GET",
			url : "removeobject",
			dataType : "json",
			data : {
				idObject : idObject,
				objectType : objectType
			},
			success : function(returnValue) {

				return returnValue; // with return value excecute code
			},
			error : function(request, error) {
				alert('error');
			}
		});
	}
}

// Called in print render info ()
// (user id --> groups + the render) --> Files
function getUserFile(idRender) {
//	var idUser = $.jStorage.get('loggedUser').idUser;
	var idUser = $("#loggedUserId").val();
	if (idUser > 0 && idRender > 0) {
		$.ajax({
			type : "GET",
			contentType : "charset=utf-8",
			url : "userfile",
			dataType : "json",
			data : {
				idUser : idUser,
				idRender : idRender
			},
			success : function(returnValue) {
				$("#filesResultRow").empty();
				var taille = returnValue.result.length;
				// alert('id Render = '+idRender+ 'id User = '+ idUser +'taille
				// = ' + taille +' file name =
				// '+returnValue.result[0].sFileName);
				$.jStorage.set('userFiles', returnValue.result);
				var i = 0;
				for (i = 0; i < taille; i++) {
					$("#filesResultRow").append(
							printFileResult(returnValue.result[i], i));
				}
				return returnValue; // with return value excecute code
			},
			error : function(request, error) {
				alert('error');
			}
		});
	}
}

function delete_File(idFile){
	var idRender = $("#renderInfoId").html();
	if(confirm('Are you sure you want to delete?')){
		if (idFile > 0) {
			$.ajax({
				type : "GET",
				url :  "deletefilebyid",
				dataType : "json",
				data :{
					idFile : idFile,
				},
				
				success : function(returnValue) {
					alert("waaaaaaaaaaaaaaaaaaaaaaaaaa3");
					getUserFile(idRender);
					return returnValue; // with return value excecute code
				},
				error : function(request, error) {
					//alert('error while deleting page');
				}
			});
		}
	}
}

function printModuleResult(module, i, storedModule) {
	var html = '';
	if (module != null) {
		html += '<button class="btn btn-block searchResultItem" onclick="printModuleInfo('
				+ i + ', \''+storedModule+'\')">';
		html += '<div class="row">';
		html += '<div class="col-md-1" style="padding-left:50px; font-size:20px;">';
		html += '<i class="fa fa-folder-open"></i>';
		html += '</div>';
		html += '<div class="col-md-3">';
		html += module.sModuleName;
		html += '</div>';
		html += '<div class="col-md-3">';
		html += module.sEnseirbId;
		html += '</div>';
		html += '<div class="col-md-3">';
		html += module.sLogin;
		html += '</div>';
		html += '</div>';
		html += '</button>';
	}
	return html;
}

function printFileResult(file, i) {
	var html = '';
	if (file != null) {
		html += '<div class="col-md-4"><div class="row">';
		html += '<div class="col-md-4">';
		html += '<a href="'
				+ file.sDirectory
				+ '"><i class="fa fa-file-text" style="font-size:100px;"></i></a>';
		html += '</div>';
		html += '<div class="col-md-6">';
		html += '<div class="row labelFileInfo">';
		html += '<p id="FileInfoName">' + file.sFileName + '</p>';
		html += '</div>';
		html += '<div class="row labelFileInfo">';
		html += '<p id="FileInfoDateDepot">' + file.dDeadlineEffective + '</p>';
		html += '</div>';
		html += '<div class="row labelFileInfo">';
		html += '<p id="FileInfoIsCorrected">';
		if (file.bIsCorrected)
			html += 'Corrig&eacute;';
		else
			html += 'Non corrig&eacute;';
		html += '</p>';
		html += '</div>';
		html += '</div>';
		html += '<div class="col-md-1">';
		html += '<a href="" onclick="delete_File('+file.idFile+');"><i class="fa fa-times"></i> </a>';
		html += '</div>';
		html += '</div></div>';

	}
	return html;
}

function printGroupResult(group, i, storedGroup) {
	var html = '';
	if (group != null) {
		html += '<button class="btn btn-block searchResultItem" onclick="printGroupInfo('
				+ i + ', \''+storedGroup+'\')">';
		html += '<div class="row">';
		html += '<div class="col-md-1" style="padding-left:50px; font-size:20px;">';
		html += '<i class="fa fa-group"></i>';
		html += '</div>';
		html += '<div class="col-md-3">';
		html += group.sGroupName;
		html += '</div>';
		html += '<div class="col-md-4">';
		html += group.sLogin;
		html += '</div>';
		html += '<div class="col-md-3">';
		html += group.sRenderName;
		html += '</div>';
		// IL MANQUE LE NOTE REPORT - A FAIRE PLUS TARD
		html += '</div>';
		html += '</button>';
	}
	return html;
}

function printRenderResult(render, i, storedRender) {
	var html = '';
	if (render != null) {
		html += '<button class="btn btn-block searchResultItem" onclick="printRenderInfo('
				+ i + ', \''+storedRender+'\')">';
		html += '<div class="row">';
		html += '<div class="col-md-1" style="padding-left:50px; font-size:20px;">';
		html += '<i class="fa fa-book"></i>';
		html += '</div>';
		html += '<div class="col-md-3">';
		html += render.sRenderName;
		html += '</div>';
		html += '<div class="col-md-4">';
		html += render.sDescription;
		html += '</div>';
		html += '<div class="col-md-3">';
		html += render.dDeadlineLimit;
		html += '</div>';
		html += '</div>';
		html += '</button>';
	}
	return html;
}

function printUserResult(user, i) {
	var html = '';
	if (user != null) {
		html += '<button class="btn btn-block searchResultItem" onclick="printUserInfo('
				+ i + ')" style="text-align:left;">';
		html += '<div class="row">';
		html += '<div class="col-md-1" style="padding-left:50px; font-size:20px;">';
		html += '<i class="fa fa-user"></i>';
		html += '</div>';
		html += '<div class="col-md-3 col-md-offset-1">';
		html += user.sFirstName + ' ' + user.sLastName;
		html += '</div>';
		html += '<div class="col-md-3">';
		html += user.sLogin;
		html += '</div>';
		html += '<div class="col-md-2">';
		html += user.sEntityName;
		html += '</div>';
		html += '<div class="col-md-2">';
		html += user.sStatusName;
		html += '</div>';
		html += '</div>';
		html += '</button>';
	}
	return html;
}

function printRenderResultSmall(render) {
	var html = '';
	if (render != null) {
		html += '<div class="userExtraInfoItem">';
		html += '<div class="row">';
		html += '<div class="col-md-5 col-md-offset-1">';
		html += render.sRenderName;
		html += '</div>';
		html += '<div class="col-md-6">';
		html += render.sModuleName;
		html += '</div>';
		html += '</div>';
		html += '</div>';
	}
	return html;
}

function printGroupResultSmall(group) {
	var html = '';
	if (group != null) {
		var membersLogins = '';
		if (group.members != null && group.members.length > 0) {
			for (var i = 0; i < group.members.length; i++) {
				membersLogins += group.members[i].sLogin + ' ';
			}
		}
		html += '<div class="userExtraInfoItem">';
		html += '<div class="row">';
		html += '<div class="col-md-1"><a href="javascript:removeUserFromGroup('+group.idGroup+');">&times;</a></div>';
		html += '<div class="col-md-3">';
		html += group.sGroupName;
		html += '</div>';
		html += '<div class="col-md-6">';
		html += membersLogins;
		html += '</div>';
		html += '</div>';
		html += '</div>';
	}
	return html;
}

function printRenderInfo(i, storedRenders) {
//	var renders = $.jStorage.get('userRenders');
	var renders = $.jStorage.get(storedRenders);
	var render = renders[i];
	if (render != null) {
		// afficher les Fichiers du groupe/rendu
		getUserFile(render.idRender);
		// adding id render to the upload form
		$("#input_id_render").val(render.idRender);
		$("#input_id_user").val($("#loggedUserId").val());
		// DEUX CHOSES
		// ON REMPLIT LA PAGE DES INFOS DU RENDU

		$("#renderInfoName").html(render.sRenderName);
		$("#renderInfoId").html(render.idRender);
		$("#renderInfoDescription").html(render.sDescription);
		$("#renderInfoDeadlineLimit").html(render.dDeadlineLimit);
		$("#renderInfoDeadlineEffective").html(render.dDeadlineEffective);
		$("#renderInfoScale").html(render.sScale);
		// ON AJOUTE LES BOUTONS DE SUPPRESSION ET EDITION DU RENDU

		var backButton = '<button type="button" class="btn btn-info btn-block" href="#" onclick="goBackFromRenderInfo()"><i class="fa fa-reply fa-lg"></i> Retour</button>';
		var editButton = '<button type="button" class="btn btn-warning btn-block" href="#"><i class="fa fa-edit fa-lg"></i> Editer</button>';
		var removeButton = '<button type="button" class="btn btn-danger btn-block" href="#"><i class="fa fa-trash-o fa-lg"></i> Supprimer</button>';
		$("#renderManageCol").empty();
		$("#renderManageCol").append(backButton);
		$("#renderManageCol").append(editButton);
		$("#renderManageCol").append(removeButton);

		// ENSUITE ON CACHE LES RESULTATS PRECEDENTS ET ON AFFICHE LA DIV DES
		// INFOS
		$("#renderResultRow").hide();
		$("#renderSearchRow").hide();
		$("#renderInfoRow").show();
	}
}

function printModuleInfo(i, storedModules) {
//	var modules = $.jStorage.get('userModules');
	var modules = $.jStorage.get(storedModules);
	var module = modules[i];
	if (module != null) {

		$("#moduleInfoName").html(module.sModuleName);
		$("#moduleInfoId").html(module.idModule);
		$("#moduleInfoEnseirbId").html(module.sEnseirbId);
		$("#moduleInfoDescription").html(module.sDescription);
		$("#moduleInfoUserId").html(module.sLogin);

		var backButton = '<button type="button" class="btn btn-info btn-block" href="#" onclick="goBackFromModuleInfo()"><i class="fa fa-reply fa-lg"></i> Retour</button>';
		var editButton = '<button type="button" class="btn btn-warning btn-block" href="#"><i class="fa fa-edit fa-lg"></i> Editer</button>';
		var removeButton = '<button type="button" class="btn btn-danger btn-block" href="#" onclick="deleteModule('
				+ module.idModule
				+ ');"><i class="fa fa-trash-o fa-lg"></i> Supprimer</button>';
		$("#moduleManageCol").empty();
		$("#moduleManageCol").append(backButton);
		$("#moduleManageCol").append(editButton);
		$("#moduleManageCol").append(removeButton);

		$("#moduleResultRow").hide();
		$("#moduleSearchRow").hide();
		$("#moduleInfoRow").show();
	}
}

function printGroupInfo(i, storedGroups) {
//	var groups = $.jStorage.get('userGroups');
	var groups = $.jStorage.get(storedGroups);
	var group = groups[i];
	if (group != null) {

		$("#groupInfoName").html(group.sGroupName);
		$("#groupInfoId").html(group.idGroup);
		$("#groupInfoCorrector").html(group.sLogin);
		$("#groupInfoRender").html(group.sRenderName);
		$("#groupInfoNoteReport").html(group.idNoteReport);

		var backButton = '<button type="button" class="btn btn-info btn-block" href="#" onclick="goBackFromGroupInfo()"><i class="fa fa-reply fa-lg"></i> Retour</button>';
		var editButton = '<button type="button" class="btn btn-warning btn-block" href="#"><i class="fa fa-edit fa-lg"></i> Editer</button>';
		var removeButton = '<button type="button" class="btn btn-danger btn-block" href="#"><i class="fa fa-trash-o fa-lg"></i> Supprimer</button>';
		$("#groupManageCol").empty();
		$("#groupManageCol").append(backButton);
		$("#groupManageCol").append(editButton);
		$("#groupManageCol").append(removeButton);

		$("#groupResultRow").hide();
		$("#groupSearchRow").hide();
		$("#groupInfoRow").show();
	}
}

function feedback(){

		
	var idRender = $("#renderInfoId").html();
	setTimeout( function() {
		$(':input','#upload_file_form').val('');
		getUserFile(idRender);
	}, 1500);
	
	
}

function printUserInfo(i) {
	var users = $.jStorage.get('userSearchResult');
	var user = users[i];
	if (user != null) {

		$("#userInfoName").html(user.sFirstName + ' ' + user.sLastName);
		$("#userInfoId").html(user.idUser);
		$("#userInfoLogin").html(user.sLogin);
		$("#userInfoMail").html(user.sMail);
		$("#userInfoEntity").html(user.sEntityName);
		$("#userInfoRight").html(user.idRight);
		$("#userInfoStatus").html(user.sStatusName);
		$("#userInfoActive").html(user.bIsActive);

		// ON AFFICHE SES RENDUS
		$("#userExtraInfoRenderRow").empty();
		var renders = user.renders;
		var nbRenders = user.renders.length;
		if (nbRenders == 0) {
			$("#userExtraInfoRenderRow")
					.append(
							'<p><i class="fa fa-frown-o"></i> Aucun rendu associ&eacute; &agrave; cet utilisateur</p>');
		} else {
			for (var i = 0; i < nbRenders; i++) {
				$("#userExtraInfoRenderRow").append(
						printRenderResultSmall(renders[i]));
			}
		}

		// ON AFFICHE SES GROUPES
		$("#userExtraInfoGroupRow").empty();
		var groups = user.groups;
		var nbGroups = user.groups.length;
		if (nbGroups == 0) {
			$("#userExtraInfoGroupRow")
					.append(
							'<p><i class="fa fa-frown-o"></i> Aucun groupe associ&eacute; &agrave; cet utilisateur</p>');
		} else {
			for (var i = 0; i < nbGroups; i++) {
				$("#userExtraInfoGroupRow").append(
						printGroupResultSmall(groups[i]));
			}
		}

		var backButton = '<button type="button" class="btn btn-info btn-block" href="#" onclick="goBackFromUserInfo();"><i class="fa fa-reply fa-lg"></i> Retour</button>';
		var editButton = '<button type="button" class="btn btn-warning btn-block" href="#"><i class="fa fa-edit fa-lg"></i> Editer</button>';
		var removeButton = '<button type="button" class="btn btn-danger btn-block" href="#"><i class="fa fa-trash-o fa-lg"></i> Supprimer</button>';
		$("#userManageCol").empty();
		$("#userManageCol").append(backButton);
		$("#userManageCol").append(editButton);
		$("#userManageCol").append(removeButton);

		$("#userSearch").hide();
		$("#userInfoRow").show();

	}
}

//CE QUI SUIS EST TRES MOCHE, IL FAUDRA Y REVENIR
function refreshUserGroup(){
	var userId = $("#loggedUserId").val();
	if(userId != null){
		getUserGroup(userId);
	}
}

function refreshUserRender(){
	var userId = $("#loggedUserId").val();
	if(userId != null){
		getUserRender(userId);
	}
}

function refreshUserModule(){
	var userId = $("#loggedUserId").val();
	if(userId != null){
		getUserModule(userId);
	}
}

function goBackFromRenderInfo() {
	$("#renderInfoRow").hide();
	$("#renderResultRow").show();
	$("#renderSearchRow").show();
}

function goBackFromModuleInfo() {
	$("#moduleInfoRow").hide();
	$("#moduleResultRow").show();
	$("#moduleSearchRow").show();
}

function goBackFromGroupInfo() {
	$("#groupInfoRow").hide();
	$("#groupResultRow").show();
	$("#groupSearchRow").show();
}

function goBackFromUserInfo() {
	$("#userInfoRow").hide();
	$("#userSearch").show();
}

function goBackFromUserCreate() {
	$("#userCreateRow").hide();
	$("#userSearch").show();
}

function goToUserCreate() {
	clearFormInputs('userCreateForm');
	$("#userSearch").hide();
	$("#userInfoRow").hide();
	$("#userCreateRow").show();
}

function goBackFromModuleCreate() {
	$("#moduleCreateRow").hide();
	$("#moduleInfoRow").hide();
	$("#moduleResultRow").show();
	$("#moduleSearchRow").show();
}

function goToModuleCreate() {
	$("#moduleResultRow").hide();
	$("#moduleInfoRow").hide();
	$("#moduleSearchRow").hide();
	$("#moduleCreateRow").show();
}

function goToGroupCreate() {
	$("#groupResultRow").hide();
	$("#groupInfoRow").hide();
	$("#groupSearchRow").hide();
	$("#groupCreateRow").show();
}

function goBackFromGroupCreate() {
	$("#groupCreateRow").hide();
	$("#groupInfoRow").hide();
	$("#groupResultRow").show();
	$("#groupSearchRow").show();
}

function goToRenderCreate() {
	$("#renderResultRow").hide();
	$("#renderInfoRow").hide();
	$("#renderSearchRow").hide();
	$("#renderCreateRow").show();
}

function goBackFromRenderCreate() {
	$("#renderCreateRow").hide();
	$("#renderInfoRow").hide();
	$("#renderResultRow").show();
	$("#renderSearchRow").show();
}

function clearFormInputs(formId) {
	// On clean les inputs
	$(':input', '#' + formId).not(':button, :submit, :reset, :hidden').val('')
			.removeAttr('checked').removeAttr('selected');
}